<?php
/**
 * Created by PhpStorm.
 * User: Laggo
 * Date: 15/10/22
 * Time: 下午7:30
 */

namespace Home\Controller;


use Home\Controller\HomeController;
use Think\Model;

class CourseController extends HomeController
{
    public $data;

    public function select()
    {
        $this->data['subject'] = M('subject')->select();
        $this->data['grade'] = M('grade')->select();
    }

    public function courseadd()
    {
        if (IS_POST) {
            $data = I('post.');
            $data['publishtime'] = date("Y-m-d H:i:s");
            $data['user_id'] = session('id');
            $result = M('course')->data($data)->add();
            if ($result) {
                if (session('type') == 1)
                    $this->success('课程添加成功', '/Bailey/index.php/Home/Teacher/account');
                if (session('type') == 3)
                    $this->success('课程添加成功', '/Bailey/index.php/Home/');
            }
        } else {
            $this->select();
            $this->assign($this->data);
            $this->display('courseadd');
        }
    }

    public function courselist()
    {
        $model = new Model();
        $userid = session('id');
        $this->data['list'] = $model->query('SELECT course.*,grade.grade_name,subject.subject_name FROM course INNER JOIN grade ON grade.id = course.grade_id INNER JOIN subject ON subject.id = course.subject_id WHERE course.user_id = ' . $userid);
        $this->assign($this->data);
        $this->display('courselist');
    }

    public function courseitemadd()
    {
        if (IS_POST) {
            $data = I('post.');
            $data['teacher_id'] = session('id');   //传入老师id
            $data['courseid'] = I('get.couseid'); //传入课程id
            $addresult = M('course_item')->data($data)->add();
            if ($addresult) {
                function manyArrTo($e,$b)
                {  //传入数组
                    $arr = array();
                    foreach ($e as $key => $val) {
                        for ($i = 0; $i < count($val); $i++) {
                            $arr[$i]['courseitemtimeid'] = $b;
                            $arr[$i][$key] = $val[$i];

                        }
                    }
                    return $arr;
                }
                $data2 = manyArrTo(I('post.cureseitemtime'),$addresult);
                $addresult2 = M('course_item_timeaddress')->addAll($data2);
                if($addresult2){
                    echo '添加成功';
                }
            }
        } else {
            $model = new Model();
            $this->data['course'] = $model->query("SELECT course.*,grade.grade_name,subject.subject_name FROM course INNER JOIN grade ON grade.id = course.grade_id INNER JOIN subject ON subject.id = course.subject_id where course.id=" . I('get.couseid'));
            $this->assign($this->data);
            $this->display('courseitemadd');
        }
    }
}